<?php
header('Content-type:text/html;charset=utf8');
/**
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-3-1
*/

/**
 * 
 * 商品控制器
 * @author xjh
 *
 */
class GoodsController extends Controller{
	/**
	 * 访问控制过滤
	 * @author xjh
	 */
	function filters(){
		return array(
			'accessControl'
		);
	}
	/**
	 * 具体方法访问具体条件
	 */
	
	function accessRules(){
		return array(
			array(
				'allow',
				'actions'=>array(),
				'users'=>array('@'),//登陆系统的用户
			),
//			array(
//				'deny',
//				'users'=>array('*'),//所有用户
//			),
		);
	}
	/**
	 * 是否有效的判断
	 */
	function actionActive($goods_id,$active_id){
		$active = new active();
		$active->setActive($goods_id,$active_id,"goods","goods_id");
		$this->redirect("./index.php?r=backweb/goods/goodshow");
	}
	
	/**
	 * 属性是否有效的判断
	 */
	function actionattrActive($attr_id,$active_id){
		$active = new active();
		$active->setActive($attr_id,$active_id,"attr","attr_id");
		echo "<script>history.back()</script>";
	}
	
/**
	 * 属性是否有效的判断
	 */
	function actionparamActive($param_id,$active_id){
		$active = new active();
		$active->setActive($param_id,$active_id,"param","param_id");
		echo "<script>history.back()</script>";
	}
	
	/**
	 * 排序信息
	 */
	function actionsort(){
		if(isset($_POST) && $_POST['send'] == "排序"){
			$goods_model = new Goods();
			$dataAll = array_combine($_POST['goods_id'],$_POST['sort']);
			$result = $goods_model->sort($dataAll);
			$this->redirect("./index.php?r=backweb/goods/goodshow");
		}
	}
	
	/**
	 * 参数配置顶部
	 */
	function actionparamtop(){
		$this->renderPartial('paramtop');
	}
	
	/**
	 * 属性配置
	 */
	function actionparam(){
		$param_model = Param::model();
		$param_info = $param_model -> allParent();
		
		if(@$_POST['btquery'] == '查询'){
			$param_info = $param_model -> pageSearch($_POST);
		}
		
		$infos_count = $param_model->infos_count;
		$page_list = $param_model->page_list;
		$obj = new CDbConnection();
		$topInfo = $obj -> getStats();
		$number = $topInfo[0];
		$times = $topInfo[1];
		
		$active[0] = "否";
		$active[1] = "是";
		$this->renderPartial('param',array('param_info'=>$param_info,'info_count'=>$infos_count[0]['count(*)'],'number'=>$number,'times'=>$times,'page_list'=>$page_list,'active'=>$active));
	}
	
	/**
	 * 属性配置顶部
	 */
	
	function actionattrtop(){
		$this->renderPartial('attrtop');
	}
	
	/**
	 * 添加属性名
	 */

	function actiontoaddAttr(){
		$this->renderPartial('addattr');
	}
	function actionaddAttr(){
		$addInfo  = $_POST;
		$attr_model = new Attr();
		$infos = $attr_model->checkExist($addInfo);
		if(isset($addInfo)){
			if($_POST['send'] == '添加'){
				if(!empty($infos)){
					echo "<script>alert('该否属性已存在');</script>";
					echo "操作错误，请返回";
					exit;
				}
				if($attr_model->addAttr($addInfo)){
					echo "<script>alert('属性添加成功');</script>";
					 $this->renderPartial('attrtop');
				}else{
					echo "<script>alert('添加失败');</script>";
				}
			}
		}
	}
	
	
	
	
	
/**
	 * 添加属性名
	 */

	function actiontoaddParam(){
		$this->renderPartial('addparam');
	}
	function actionaddParam(){
		$addInfo  = $_POST;
		$param_model = new Param();
		$infos = $param_model->checkExist($addInfo);
		if(isset($addInfo)){
			if($_POST['send'] == '添加'){
				if(!empty($infos)){
					echo "<script>alert('该参数已存在');</script>";
					echo "操作错误，请返回";
					exit;
				}
				if($param_model->addParam($addInfo)){
					echo "<script>alert('参数添加成功');</script>";
					 $this->renderPartial('paramtop');
				}else{
					echo "<script>alert('添加失败');</script>";
				}
			}
		}
	}
	
	
	
	
	
	/**
	 * 属性配置
	 */
	function actionattr(){
		$attr_model = Attr::model();
		$attr_info = $attr_model -> allParent();
		
		if(@$_POST['btquery'] == '查询'){
			$attr_info = $attr_model -> pageSearch($_POST);
		}
		
		$infos_count = $attr_model->infos_count;
		$page_list = $attr_model->page_list;
		$obj = new CDbConnection();
		$topInfo = $obj -> getStats();
		$number = $topInfo[0];
		$times = $topInfo[1];
		
		$active[0] = "否";
		$active[1] = "是";
		$this->renderPartial('attr',array('attr_info'=>$attr_info,'info_count'=>$infos_count[0]['count(*)'],'number'=>$number,'times'=>$times,'page_list'=>$page_list,'active'=>$active));
	}
	
	/**
	 * 参数值
	 */
	function actionparamValue($param_id){
		$param_model = Param::model();
		$paramvalue_info = $param_model -> getParentValue($param_id);
		$active = array(
			1=>'是',
			2=>'否'
		);
		$this->renderPartial('paramValue',array('paramvalue_info'=>$paramvalue_info,'param_id'=>$param_id));
	}
	
	/**
	 * 属性值
	 */
	function actionattrValue($attr_id){
		$attr_model = Attr::model();
		$attrvalue_info = $attr_model -> getParentValue($attr_id);
		
		$active = array(
			1=>'是',
			2=>'否'
		);
		$this->renderPartial('attrValue',array('attrvalue_info'=>$attrvalue_info,'attr_id'=>$attr_id));
	}
	
/**
	 * 参数值增加
	 */
	function actionparamValueAdd($param_id){
		$param_model = Param::model();
		$infos = $param_model->checkExist($_POST);
		
		if(isset($_POST) && $_POST['send'] == '添加'){
				if(!empty($infos)){
						echo "<script>alert('该参数已存在');</script>";
						echo "操作错误，请返回";
						exit;
				}
				if($param_model->addParamValue($param_id,$_POST)){
					echo "<script>alert('参数值添加成功');</script>";
				}else{
					echo "<script>alert('添加失败');</script>";
				}
		} 
		
		$paramvalue_info = $param_model -> getParentValue($param_id);
	
		$this->renderPartial('paramValue',array('paramvalue_info'=>$paramvalue_info,'param_id'=>$param_id));
	}
	
	
	/**
	 * 属性值增加
	 */
	function actionattrValueAdd($attr_id){
		$attr_model = Attr::model();
		$infos = $attr_model->checkExist($_POST);
		
		if(isset($_POST) && $_POST['send'] == '添加'){
				if(!empty($infos)){
						echo "<script>alert('该否属性已存在');</script>";
						echo "操作错误，请返回";
						exit;
				}
				if($attr_model->addAttrValue($attr_id,$_POST)){
					echo "<script>alert('属性值添加成功');</script>";
				}else{
					echo "<script>alert('添加失败');</script>";
				}
		} 
		
		$attrvalue_info = $attr_model -> getParentValue($attr_id);
	
		$this->renderPartial('attrValue',array('attrvalue_info'=>$attrvalue_info,'attr_id'=>$attr_id));
	}
	
	/**
	 * 参数修改
	 */
/**
	 * 属性名修改
	 */
	public function actionparamupdate($param_id){
		$param_model = Param::model();
		$infos = $param_model->getParamById($param_id);
		$paramvalue_parent_id = $infos[0]['parent_id'];
		if(isset($_POST) && @$_POST['send'] == '修改'){
			$row = $param_model -> updateParam($param_id,$_POST);
			if($row){
				echo "<script>alert('参数值修改成功')</script>";
				if($paramvalue_parent_id == 0){
					echo "<script>location.href='./index.php?r=backweb/goods/param';</script>";  
				}else{
					echo "<script>location.href='./index.php?r=backweb/goods/paramvalue&param_id='+$paramvalue_parent_id;</script>";  
				}
			}else{
				echo "<script>alert('修改失败');</script>";
				echo "操作错误,请返回继续操作";
				exit;
			}
		}
		
		$this->renderPartial('paramupdate',array('param_id'=>$param_id,'infos'=>$infos[0],'parent_id'=>$paramvalue_parent_id));
	}
	
	/**
	 * 属性名修改
	 */
	public function actionattrupdate($attr_id){
		$attr_model = Attr::model();
		$infos = $attr_model->getAttrById($attr_id);
		$attrvalue_parent_id = $infos[0]['parent_id'];
		if(isset($_POST) && @$_POST['send'] == '修改'){
			$row = $attr_model -> updateAttr($attr_id,$_POST);
			if($row){
				echo "<script>alert('属性值修改成功')</script>";
				if($attrvalue_parent_id == 0){
					echo "<script>location.href='./index.php?r=backweb/goods/attr';</script>";  
				}else{
					echo "<script>location.href='./index.php?r=backweb/goods/attrvalue&attr_id='+$attrvalue_parent_id;</script>";  
				}
			}else{
				echo "<script>alert('修改失败');</script>";
				echo "操作错误,请返回继续操作";
				exit;
			}
		}
		
		$this->renderPartial('attrupdate',array('attr_id'=>$attr_id,'infos'=>$infos[0],'parent_id'=>$attrvalue_parent_id));
	}
	
/**
	 * 参数删除
	 */
	public function actionparamdel($param_id){
		$param_model = Param::model();
		$infos = $param_model->getParamById($param_id);
		$paramvalue_parent_id = $infos[0]['parent_id'];
		$row = $param_model->paramdel($param_id);
		if($row){
			echo "<script>alert('删除成功');</script>";
			if($paramvalue_parent_id == 0){
				echo "<script>location.href='./index.php?r=backweb/goods/param';</script>";  
			}else{
				echo "<script>location.href='./index.php?r=backweb/goods/paramvalue&param_id='+$paramvalue_parent_id;</script>";  
			}
		}
	}
	
	
	/**
	 * 属性名删除
	 */
	public function actionattrdel($attr_id){
		$attr_model = Attr::model();
		$infos = $attr_model->getAttrById($attr_id);
		$attrvalue_parent_id = $infos[0]['parent_id'];
		
		$row = $attr_model->attrdel($attr_id);
		if($row){
			echo "<script>alert('删除成功');</script>";
			if($attrvalue_parent_id == 0){
				echo "<script>location.href='./index.php?r=backweb/goods/attr';</script>";  
			}else{
				echo "<script>location.href='./index.php?r=backweb/goods/attrvalue&attr_id='+$attrvalue_parent_id;</script>";  
			}
		}
	}
	/**
	 * 商品信息
	 * @author xjh
	 */
	function actiongoodShow($brand_id = 0,$category_id = 0){
		$search = $_POST;
		$goods_model = Goods::model();
		$brand_info = $goods_model->getGoodsBrand();
		$category_info = $goods_model->getGoodsCategory();
		if(Yii::app()->user->name != ''){
			$lg_model = new LoginForm();
			$role_name = $lg_model->getRoleName();
		}
		$result = $goods_model->searchContent($search,$brand_id,$category_id,$role_name[0]['role_name']);
		$page_list = $goods_model->page_list;
		foreach($category_info as $_k => $_v){
			$category_info[$_v['category_id']] = $_v['category_name'];
		}
		foreach ($brand_info as $_k => $_v){
			$brand_info[$_v['brand_id']] = $_v['brand_name'];
		}
		$active[0] = "否";
		$active[1] = "是";
		$topInfo = @CDbConnection::getStats();
		$number = $topInfo[0];
		$times = $topInfo[1];
		$info_count = $goods_model->infos_count[0]['count(*)'];
		
		$this->renderPartial('goodshow',array('infos'=>$result,'brand_info'=>$brand_info,'category_info'=>$category_info,'active'=>$active,'page_list'=>$page_list,'number'=>$number,'times'=>$times,'info_count'=>$info_count,'user_type'=>$role_name[0]['role_name']));
	}
	
	
	
	/**
	 * 顶部信息
	 * @author xjh
	 */
	function actiongoodsTop(){
		$this->renderPartial('GoodsTop');
	}	
	function actiontoaddGoods(){
		$store_model = Store::model();
		$store_info = $store_model->getUserStore(Yii::app()->user->name);
		if(Yii::app()->user->name != ''){
			$lg_model = new LoginForm();
			$role_name = $lg_model->getRoleName();
		}
		$this->renderPartial('addgoods',array('store_info'=>$store_info,'user_type'=>$role_name[0]['role_name']));
	}
	
	
	
	/**
	 * 新增商品控制器
	 * @author xjh
	 */
	function actionaddGoods(){
		$addInfo = $_POST;
		
		if(!empty($addInfo['goods_thumbnail_url'])){
			$image = new Image($addInfo['goods_thumbnail_url']);
	//		$image->thumb(50);
			$image->fixPro(191,180);
			$image->out();
		}
		$goods_model = new Goods();
		if(@$addInfo['send'] == "添加"){
		
			if($goods_model->checkExist($addInfo)){
				echo "<script>alert('商品已存在');</script>";
					$this->renderPartial('addGoods');
			}else{
				
			//事务处理
			$model=Goods::model();
			$transaction=$model->dbConnection->beginTransaction();
			try
			{
			   $row = $model->addGoods($addInfo);
			   $new_id = Yii::app()->db->getLastInsertID();
			   $ro = $model->addImages($addInfo,$new_id);
			   $r = $model -> addToStore($addInfo,$new_id);
			   $transaction->commit();
			}
			catch(Exception $e) //一条失败就回滚
			{
			  	$transaction->rollBack();
			}
				if($r){
					echo "<script>alert('添加成功');</script>";
					$this->renderPartial('goodsTop');
				}else{
					echo "<script>alert('添加失败');</script>";
				}
			}
		}
		
	}
	//单文件上传
	function actionthumbnail(){
		$this->renderPartial('thumbnail');
	}
	//多文件上传
	function actionmulupload(){
		
		
		echo "<form enctype='multipart/form-data' method='post' name='upform'>";
		echo "<input name='upfile' style='text-align:center;margin:20px; 0px 0px 20px' type='file' size='30' >";
		echo "<input type='submit' style='background:#ccc;width:70px;height:30px;' value='上传'><br>";
		echo "<span style='font-size:13px; color:#F00;'>"."允许上传的文件类型为：jpg, jpeg, png, pjpeg, gif, bmp, x-png</span>";
		echo '</form>';
		
		$id =  $_GET['label'];
		$max_file_size=20000000;     //上传文件大小限制, 单位BYTE
		$destination_folder=ROOT_PATH.UPLOAD_URL.'details_img'; //上传文件路径
		$today = date("Ymd");
		$all_path = $destination_folder.$today.'/';
		$allowtypes = array(
			'image/jpg',
			'image/jpeg',
			'image/png',
			'image/pjpeg',
			'image/gif',
			'image/bmp',
			'image/x-png',
		);
		//判断提交方式
		if($_SERVER['REQUEST_METHOD'] == "POST"){
			if(!is_uploaded_file($_FILES['upfile']['tmp_name'])){
				echo "图片不存在";
				exit;
			}
			
			if($_FILES['upfile']['size'] > $max_file_size){
				echo "文件大小超过规定值,请重新上传";
				exit;
			}
			
			if(!in_array($_FILES['upfile']['type'],$allowtypes)){
				echo "文件类型不符合,请重新上传";
				exit;
			}
			
			if(!file_exists($all_path)){
				mkdir($all_path);
			}
			
			$filename = $_FILES['upfile']['tmp_name'];
			$image_size = getimagesize($filename);
			$pinfo=pathinfo($_FILES['upfile']["name"]);
    		$ftype=$pinfo['extension'];
			
    		$all_path = $all_path.$today.time().".".$ftype;
			if(file_exists($all_path) && $overwrite != true){
				echo "文件已存在";
				exit;
			}
			
			if(!move_uploaded_file($filename,$all_path)){
				echo "移动出错";
				exit;
			}
			
			$pinfo = pathinfo($all_path);
			$fname = $pinfo['basename'];
			if(isset($id)){
				$_path = $_SERVER["SCRIPT_NAME"];
				$_dir = dirname($_path);
		 		if($_dir == '\\') $_dir = '/';
		 		$all_path = $_dir.UPLOAD_URL.'details_img'.$today.'/'.$today.time().".".$ftype;
		 		echo $all_path;
				echo "<script type='text/javascript'>var path='".$all_path."'</script>";
				echo "<script  type='text/javascript'>window.parent.document.getElementById('pic_url$id').value = path</script>";
				echo "<script  type='text/javascript'>alert('上传成功')</script>";
				echo "<script  type='text/javascript'>window.parent.document.getElementById('img$id').value = path</script>";
				echo "<script  type='text/javascript'>window.parent.document.getElementById('light').style.display='none'</script>";
				echo "<script  type='text/javascript'>window.parent.document.getElementById('fade').style.display='none'</script>";
				echo "<script>window.close();</script>";
			}
		}
	}
	
	//单文件上传处理
	function actionuploadfile(){ 
		if(isset($_POST['send'])){
			$_fileupload = new upload('pic',$_POST['MAX_FILE_SIZE']);
			$this->alertOpenerClose("缩略图上传成功",$_fileupload->getPath());
		}else{
			echo "<script>alert('文件过大,请重新上传')</script>";
			exit;
		}
	}
	
	
	
	/**
	 * 编辑器图片
	 */
	function actionCkedit(){
		if(isset($_GET)){
			$_fileupload = new upload('upload',$_POST['MAX_FILE_SIZE']);
			$_ckefn = $_GET['CKEditorFuncNum'];
			$_path = $_fileupload->getPath();
			$_img = new Image($_path);
			$_img -> ckeImg(700,0);
			$_img->out(); 
			echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($_ckefn,\"$_path\",'图片上传成功!');</script>";
			exit();
		}else{
			echo "<script>alert('上传错误');</script>";
			exit;
		}
	}
	/**
	 * 修改商品控制器
	 * @author xjh
	 */
	function actionUpdateGoods($goods_id){
		
		$rec_info = $_POST;
		$cmodel = new CCCModel();
		$goods_model = Goods::model();
		$rst = $goods_model->getGoodsDetailsImg($goods_id);
		$row = $goods_model -> getGoodsById($goods_id);
		$category_id = $row[0]['category_id'];
		$info = $goods_model->categoryAll();
		$info2 = $goods_model->brandAll($category_id);
		$category_name = $info[$category_id];
		@$rec_info['brand_id'] = $rec_info['brand'];
		if(Yii::app()->user->name != ''){
			$lg_model = new LoginForm();
			$role_name = $lg_model->getRoleName();
		}
		$store_model = Store::model();
		$store_info = $store_model->getUserStore(Yii::app()->user->name);
		
		$store_id = $goods_model->getStoreId($goods_id);
		$store_id = $store_id[0]['store_id'];
		if(!is_numeric(@$rec_info['cat'])){
			$rec_info['category_id'] = $row[0]['category_id'];
		}
		else{
			$rec_info['category_id'] = $rec_info['cat'];
		}
		if(@$_POST['send'] == "修改" && isset($_POST)){
		
			//----sql事务处理------
			//事务处理
			$model=Goods::model();
			$transaction=$model->dbConnection->beginTransaction();
			try
			{
			   $row = $model-> updateGoods($rec_info,$goods_id);
			   $ro = $model-> updateImages($_POST['picpic'],$goods_id);
			   $r = $model -> updateGoodsStore($rec_info,$goods_id);
			   
			   $transaction->commit();
			}
			catch(Exception $e) //一条失败就回滚
			{
			  	$transaction->rollBack();
			  	print_r($e);
			  	exit;
			}
			//----sql事务处理------
			if($row){
				echo "<script>alert('修改成功');</script>";
				echo "<script>location.href='./index.php?r=backweb/goods/goodstop'</script>";
			}else{
				echo "<script>alert('修改失败');</script>";
			}
		}
		$this->renderPartial('updategoods',array('info'=>$row[0],'catid'=>$category_id,'catname'=>$category_name,'brainfo'=>$info2,'details_img'=>$rst,'user_type'=>$role_name[0]['role_name'],'store_info'=>$store_info,'store_id'=>$store_id));
	}
	
	/**
	 * 为商品配置属性参数
	 */
	function actiongoodsParamConfig($goods_id){
		$param_model = Param::model();
		$goods_model = Goods::model();
		$checked_param = $goods_model->ParamList($goods_id);
		$styleinfo = $goods_model->getStyleInfo($goods_id);
		$param_info = $param_model->allParent();
		if(isset($_POST) && @$_POST['send'] == '设置'){
			$row = $goods_model->updParam($goods_id,$_POST['param']);
			echo "<script>location.href='./index.php?r=backweb/goods/goodsParamConfig&goods_id=$goods_id'</script>";
		}
		$this->renderPartial('goodsParamConfig',array('styleinfo'=>$checked_param,'goods_id'=>$goods_id));
	}
	
	
	/**
	 * 为商品配置属性信息
	 */
	function actiongoodsAttrConfig($goods_id){
		$attr_model = Attr::model();
		$goods_model = Goods::model();
		$checked_attr = $goods_model->AttrList($goods_id);
		$styleinfo = $goods_model->getStyleInfo($goods_id);
		$attr_info = $attr_model->allParent();
		if(isset($_POST) && @$_POST['send'] == '设置'){
			$row = $goods_model->updAttr($goods_id,$_POST['attr']);
			echo "<script>location.href='./index.php?r=backweb/goods/goodsAttrConfig&goods_id=$goods_id'</script>";
		}
		$this->renderPartial('goodsAttrConfig',array('styleinfo'=>$checked_attr,'goods_id'=>$goods_id));
	}
	
	
	/**
	 * 删除商品控制器
	 * @author xjh
	 */
	function actiondelGoods($goods_id){
		$goods_model = Goods::model();
		$row = $goods_model->deleteGoods($goods_id);
		if($row){
			$this->redirect("./index.php?r=backweb/goods/goodshow");
		}else{
			echo "<script>alert('商品删除失败');</script>";
		}
	}
	
	
	function alertOpenerClose($_info,$_path){
		echo "<script type='text/javascript'>alert('$_info');</script>";
		echo "<script type='text/javascript'>opener.document.fm.goods_thumbnail_url.value='$_path';</script>";
		echo "<script type='text/javascript'>opener.document.fm.pic.style.display='block';</script>";
		echo "<script type='text/javascript'>opener.document.fm.pic.src='$_path';</script>";
		echo "<script type='text/javascript'>window.close()</script>";
		exit();
	}
	
	
	
}
?>